package org.xxx.epms.models.epms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxx.epms.models.epms.dto.AqiFeedbackParam;
import org.xxx.epms.models.epms.dto.InspectorParam;
import org.xxx.epms.models.epms.model.AqiFeedback;
import org.xxx.epms.models.epms.model.Inspector;
import org.xxx.epms.models.epms.mapper.InspectorMapper;
import org.xxx.epms.models.epms.model.Monitor;
import org.xxx.epms.models.epms.service.InspectorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lb
 * @since 2024-06-08
 */
@Service
public class InspectorServiceImpl extends ServiceImpl<InspectorMapper, Inspector> implements InspectorService {

    @Autowired
    private InspectorMapper inspectorMapper;

    private QueryWrapper<Inspector> getQueryWrapper(InspectorParam inspectorParam){
        QueryWrapper<Inspector> wrapper = new QueryWrapper<>();
        if(inspectorParam.getInsId() != null){
            wrapper.lambda().eq(Inspector::getInsId, inspectorParam.getInsId());
        }
        if(inspectorParam.getInsName() != null){
            wrapper.lambda().like(Inspector::getInsName, inspectorParam.getInsName());
        }
        if(inspectorParam.getInsTel() != null){
            wrapper.lambda().eq(Inspector::getInsTel, inspectorParam.getInsTel());
        }
        if(inspectorParam.getProvinceId() != null){
            wrapper.lambda().eq(Inspector::getProvinceId, inspectorParam.getProvinceId());
        }
        if(inspectorParam.getCityId() != null){
            wrapper.lambda().eq(Inspector::getCityId, inspectorParam.getCityId());
        }
        if(inspectorParam.getInsAddress() != null){
            wrapper.lambda().eq(Inspector::getInsAddress, inspectorParam.getInsAddress());
        }
        if(inspectorParam.getRemarks() != null){
            wrapper.lambda().like(Inspector::getRemarks, inspectorParam.getRemarks());
        }
        return wrapper;
    }


    @Override
    public Page<Inspector> getInspectorList(InspectorParam inspectorParam, int pageNum, int pageSize) {
        if(inspectorParam == null){
            Page<Inspector> page = new Page<>(pageNum, pageSize);
            return page(page, null);
        }else{
            QueryWrapper<Inspector> wrapper = getQueryWrapper(inspectorParam);
            Page<Inspector> page = new Page<>(pageNum, pageSize);
            return page(page, wrapper);
        }
    }

    @Override
    public int getInspectorListTotalPage(InspectorParam inspectorParam, int pageSize) {
        if(inspectorParam.getInsId() == null){
            //计算总页数
            long total = inspectorMapper.selectCount(null);
            return (int)total / pageSize + (total % pageSize > 0? 1 : 0);
        }else {
            QueryWrapper<Inspector> wrapper = getQueryWrapper(inspectorParam);
            long total = inspectorMapper.selectCount(wrapper);
            return (int) total / pageSize + (total % pageSize > 0 ? 1 : 0);
        }
    }

    @Override
    public Inspector getInspectorById(Integer insId) {
        QueryWrapper<Inspector> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Inspector::getInsId, insId);
        Inspector inspector = inspectorMapper.selectOne(wrapper);
        return inspector;
    }

}
